<?php

namespace app\admin\controller;

use app\admin\model\QResult;
use app\admin\model\Jour as JourModel;

use think\Db;
use think\Request;

class JourController extends BaseController{
    //列表
    public function lst(){
        $map = [];
        $params=$this->request->param();
        if(!empty($params['name']) )
        {
            $map['name']=array('like','%'.$params['name'].'%');
        }else{
            $map=[];
        }
        $data = JourModel::where('delete_time',null)
            ->where('status',1)
            ->order('orders desc')
            ->where($map)
            ->limit(3)
            ->paginate(10);

        $this->assign([
            'data'=>$data,
        ]);
        return $this->fetch('jour/lst');
    }

    //添加
    public function add(){

        if(request()->isGet()){
            return $this->fetch('jour/add');
        }
        if($this->request->isPost()){
            $request = Request::instance();
            $params = $this->request->param();
            $controller = strtolower($request->controller());//生成控制器名字文件夹
            if(request()->file('pic')){
                $picInfo = uploadOne($controller,'thumb','pic',150,150);
                $params['pic'] = $picInfo['pic'];
                $params['sm_pic'] = $picInfo['thumb_pic'];
            }
            if(empty($params['pic'])){
                $this->error('亲,图片还未上传!');
            }
            //var_dump($params);die;
            $model = new JourModel();
            if($model->validate(true)->allowField(true)->save($params)){
                $this->success('添加成功',url('admin/jour/lst'),'',1);
            }else{
                $this->error($model->getError() );
            }
        }
        return '非法操作';

    }

    //删除
    public function del()
    {
        $id = $this->request->param('id');
        $info = JourModel::where(['id'=>$id])->find();

        @unlink($info['pic']);
        @unlink($info['sm_pic']);
        if(JourModel::where(['id'=>$id])->delete()){
            $this->success('删除成功!',url('admin/jour/lst'),'',1);
        }else{
            $this->error('删除失败!');
        }
    }

    //修改
    public function edit()
    {
        if($this->request->isGet()) {
            //$id = $this->request->param('id');
           // var_dump($id);die;
            $params = $this->request->param();
            $this->assign('data',JourModel::find($params['id']));
            return $this->fetch('jour/edit');
        }
        if($this->request->isPost()){
            $request = Request::instance();
            $id = $this->request->param('id');
            $params = $this->request->param();
            $controller = strtolower($request->controller());//生成控制器名字文件夹
            if(request()->file('pic')){
                $picInfo = uploadOne($controller,'thumb','pic',150,150);
                $params['pic'] = $picInfo['pic'];
                $params['sm_pic'] = $picInfo['thumb_pic'];
            }

           // $info = JourModel::where(['id'=>$id])->find();
            $model = new JourModel();
            if($model->validate(true)->allowField(true)->isUpdate(true)->save($params)){
//                @unlink($info['pic']);
//                @unlink($info['sm_pic']);
                $this->success('添加成功',url('admin/jour/lst'),'',1);
            }else{
                $this->error($model->getError() );
            }
        }

    }


}